package com.android.tea.shopper;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.tea.R;
import com.android.tea.TeaEncyclopediaApp;
import com.android.tea.api.ApiResult;
import com.android.tea.api.ApiReturnResultListener;
import com.android.tea.api.TeaApi;
import com.android.tea.entity.TeaCompany;
import com.android.tea.image.ImageLoaderManager;
import com.android.tea.util.StringUtils;
import com.android.tea.util.TeaLog;
import com.android.tea.util.Utils;

public class ShopperDetailsActivity extends Activity {
	String id = null;
	public final static String TAG = "ShopperDetailsActivity";
	RelativeLayout item;
	ImageView ivAvatar;
	TextView tvTitle;
	TextView tvProduct;
	TextView tvSetEmail;
	TextView tvPhone;
	TextView tvRegion;
	TextView tvAvageCost;
	ImageButton ibnewBack;
	Gallery gallery;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.good_shooper_details);
		TeaEncyclopediaApp.list.add(this);
		
		Intent intent = getIntent();
		id = intent.getStringExtra("id");
		TeaLog.e(TAG, "id" + id);
		initComponents();
		getDetailsID(id);
	}

	private ImageLoaderManager imageLoaderManager;

	private void getDetailsID(String id) {
		final ProgressDialog myDialog;
		myDialog = ProgressDialog.show(ShopperActivity.instance, "请稍候",
				"正在获取详细信息...", true);
		myDialog.setCancelable(true);
		myDialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {

				finish();
			}
		});
		if (null != id) {
			TeaApi teaApi = new TeaApi(this);
			teaApi.setReturnResultListener(new ApiReturnResultListener() {

				@Override
				public <T> void onReturnSucceedResult(int requestCode,
						ApiResult<T> apiResult) {

					TeaCompany shopperMember = (TeaCompany) apiResult
							.getEntity();
					if (shopperMember != null) {
						imageLoaderManager = new ImageLoaderManager(
								ShopperDetailsActivity.this, new Handler(),
								ivAvatar);
						ivAvatar.setImageBitmap(imageLoaderManager.getImage(
								shopperMember.getCompanyLogo().trim(),
								R.drawable.hu, Utils.dipToPixels(
										ShopperDetailsActivity.this, 74)));
						setTitle(tvTitle, shopperMember.getCompanyName());
						setTitle(tvPhone, "联系电话:"
								+ shopperMember.getCompanyTel());
						setTitle(tvProduct, "主营产品:"
								+ shopperMember.getCoinName());
						setTitle(tvRegion, "地区:"
								+ shopperMember.getCompanyCityCode());
						setTitle(tvSetEmail, "邮箱:"
								+ shopperMember.getCompanyEmail());
						setTitle(tvAvageCost, "人均消费:"
								+ shopperMember.getCompanyCaco());
						String[] urlStrings = shopperMember.getCompanyImages()
								.split(",");
						gallery.setAdapter(new ImageAdapter(
								ShopperDetailsActivity.this, urlStrings));

					} else {
						finish();
						Toast.makeText(ShopperDetailsActivity.this, "获取详情失败。",
								Toast.LENGTH_LONG).show();

					}
					myDialog.dismiss();
				}

				@Override
				public <T> void onReturnFailResult(int requestCode,
						ApiResult<T> apiResult) {
					finish();
					myDialog.dismiss();
					Toast.makeText(ShopperDetailsActivity.this, "获取详情失败。",
							Toast.LENGTH_LONG).show();

				}
			});
			teaApi.getTeaDetail(0, id);
		}
	}

	private void setTitle(TextView tvTitle, String title) {
		if (StringUtils.isNotNullOrEmpty(title)) {
			tvTitle.setText(title);
		} else {
			tvTitle.setText(this.getResources().getString(R.string.unknown));
		}
	}

	private void initComponents() {
		ibnewBack = (ImageButton) findViewById(R.id.btn_new_back);
		gallery = (Gallery) findViewById(R.id.picList);

		ibnewBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		item = (RelativeLayout) findViewById(R.id.shopperItem);
		ivAvatar = (ImageView) findViewById(R.id.shopperItemImage);
		tvTitle = (TextView) findViewById(R.id.shopperName);
		tvPhone = (TextView) findViewById(R.id.phoneNumber);
		tvProduct = (TextView) findViewById(R.id.shopperPruduct);
		tvRegion = (TextView) findViewById(R.id.shopperLocal);
		tvSetEmail = (TextView) findViewById(R.id.shopperEmail);
		tvAvageCost = (TextView) findViewById(R.id.shopperAverageCost);
	}

	private class ImageAdapter extends BaseAdapter {
		private Context mContext;
		private String[] mImage;
		ImageLoaderManager imageLoaderManager;

		public ImageAdapter(Context c, String[] urlStrings) {
			mContext = c;
			imageLoaderManager = new ImageLoaderManager(mContext,
					new Handler(), this);
			mImage = urlStrings;

		}

		@Override
		public int getCount() {

			return mImage.length;
		}

		@Override
		public String getItem(int position) {

			return mImage[position];
		}

		@Override
		public long getItemId(int position) {

			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ImageView i = new ImageView(mContext);

			i.setImageBitmap(imageLoaderManager.getImage(getItem(position)
					.trim(), R.drawable.hu, Utils.dipToPixels(
					mContext, 200)));
			return i;
		}

	};
}
